package com.itchu.system.service.impl;

import com.itchu.common.exption.ExceptionEnum;
import com.itchu.common.exption.MusicException;
import com.itchu.system.domain.Comment;
import com.itchu.system.mapper.CommentMapper;
import com.itchu.system.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    /**
     * 添加评论
     * @param comment
     * @return
     */
    @Override
    public Integer saveComent(Comment comment) {
        comment.setCreateTime(new Date());
        int i = commentMapper.insertSelective(comment);
        return i;
    }

    /**
     * 查询歌单的评论
     * @param songlistId
     * @return
     */
    @Override
    public List<Comment> getSongListComments(Long songlistId) {

        List<Comment> list = commentMapper.getCommentsByListId(songlistId);

        if (CollectionUtils.isEmpty(list)) {
            throw new MusicException(ExceptionEnum.MUSIC_NOT_COMMENT);
        }

        return list;
    }

    @Override
    public List<Comment> getSongComments(Long songId) {
        List<Comment> list = commentMapper.getSongComments(songId);

        if (CollectionUtils.isEmpty(list)) {
            throw new MusicException(ExceptionEnum.MUSIC_NOT_COMMENT);
        }

        return list;
    }
}
